window.onload = function () {
    showLoginInfo();
    showProduct(1);
}


function showProduct(pageNO) {
    axios.get("/product/findByItem",{
        params: {
            pageNO,
            name:$("name").value,
            startDate:$("startDate").value,
            endDate:$("endDate").value
        }
    }).then(e => {
        var info = e.data;
        //显示商品数据
        var str = "";
        for (var i = 0;i < info.records.length;i++){
            var obj = info.records[i];
            str += `<div>
                        <img src="/productImage/${obj.imgPath}" width="200" height="200"><br>
                        ${obj.name}<br>
                        单价：${obj.price}<br>
                        已售出：${obj.sum}件<br>
                        <a href="/custom/shop.html?productId=${obj.id}">购买</a>
                        <a href="/product.html?productId=${obj.id}">查看</a>
                    </div>`
        }
        $("productDiv").innerHTML = str;

        var cutStr = "";
        for (var i=1;i<=info.pages;i++){
            cutStr += `<a href="javascript:showProduct(${i})">${i}</a>`
        }
        $("cutPageDiv").innerHTML = cutStr;
    });

}

/**
 * 显示登录信息
 */
function showLoginInfo() {
    axios.get("/user/getLoginUser",null).then(e => {
        var info = e.data
        if (info != null && info != ''){
            $("infoDiv").innerHTML = "欢饮光临" + info.name;
        }else {
            $("infoDiv").innerHTML = "你好，请先<a href='/login.html'>登录</a>"
        }
    })
}
